﻿Public Class danhsachnhomhang

    Private NhomSanPhamBS As NhomSanPhamBS
    Private NhomSanPhamGD As NhomSanPhamGD
    Private cm As CurrencyManager

    '---------------- PageLoad

    Private Sub danhsachnhomhang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDGV_DSNhomHang()
        dgrvDSNhomHang.Columns(0).HeaderText = "Mã Nhóm SP"
        dgrvDSNhomHang.Columns(1).HeaderText = "Tên Nhóm SP"

        'Dim NhomSanPhamList As New List(Of NhomSanPhamGD)
        'NhomSanPhamList = NhomSanPhamBS.GetData()
        'cm = Me.BindingContext(NhomSanPhamList)
        'txtmanhomhang.DataBindings.Add("Text", NhomSanPhamList, "MaNhomSanPham")
        'txttennhomhang.DataBindings.Add("Text", NhomSanPhamList, "TenNhomSanPham")
        'dgrvDSNhomHang.DataSource = NhomSanPhamList

        dgrvDSNhomHang.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    End Sub


    '---------------- Load

    Private Sub LoadDGV_DSNhomHang()
        Dim NhomSanPhamList As New List(Of NhomSanPhamGD)
        NhomSanPhamBS = New NhomSanPhamBS
        NhomSanPhamList = NhomSanPhamBS.GetData()
        dgrvDSNhomHang.DataSource = NhomSanPhamList
        dgrvDSNhomHang.ClearSelection()
    End Sub

    Private Sub ClearTextBox()
        txtmanhomhang.Text = ""
        txttennhomhang.Text = ""
    End Sub

    Private Sub ClearBinding()
        txtmanhomhang.DataBindings.Clear()
        txttennhomhang.DataBindings.Clear()
    End Sub


    '------------- Các Button


    Private Sub btnthemnhomhang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthemnhomhang.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        btncapnhatnhomhang.Visible = False
        ClearTextBox()
    End Sub

    Private Sub btnsuanhomhang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuanhomhang.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        btnaddnhomhang.Visible = False
        txtmanhomhang.Enabled = False

        Dim maNhomSanPham As String
        maNhomSanPham = dgrvDSNhomHang.SelectedRows.Item(0).Cells(0).Value
        Dim tenNhomSanPham As String
        tenNhomSanPham = dgrvDSNhomHang.SelectedRows.Item(0).Cells(1).Value

        txtmanhomhang.Text = maNhomSanPham
        txttennhomhang.Text = tenNhomSanPham

    End Sub

    Private Sub btnxoanhomhang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoanhomhang.Click
        Dim maNhomSanPham As String
        maNhomSanPham = dgrvDSNhomHang.SelectedRows.Item(0).Cells(0).Value
        Dim tenNhomSanPham As String
        tenNhomSanPham = dgrvDSNhomHang.SelectedRows.Item(0).Cells(1).Value
        NhomSanPhamGD = New NhomSanPhamGD(maNhomSanPham, tenNhomSanPham)
        NhomSanPhamBS.Delete(NhomSanPhamGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSNhomHang()
    End Sub

    Private Sub btndongnhomhang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndongnhomhang.Click
        Me.Close()
    End Sub

    Private Sub btnaddnhomhang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddnhomhang.Click
        Dim maNhomSanPham As String = txtmanhomhang.Text
        Dim tenNhomSanPham As String = txttennhomhang.Text
        NhomSanPhamGD = New NhomSanPhamGD(maNhomSanPham, tenNhomSanPham)
        NhomSanPhamBS.Insert(NhomSanPhamGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSNhomHang()
    End Sub

    Private Sub btnbacknhomhang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbacknhomhang.Click
        ClearBinding()
        LoadDGV_DSNhomHang()
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        btncapnhatnhomhang.Visible = True
        btnaddnhomhang.Visible = True
    End Sub

    Private Sub btncapnhatnhomhang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapnhatnhomhang.Click
        Dim maNhomSanPham As String = txtmanhomhang.Text
        Dim tenNhomSanPham As String = txttennhomhang.Text
        NhomSanPhamGD = New NhomSanPhamGD(maNhomSanPham, tenNhomSanPham)
        NhomSanPhamBS.Update(NhomSanPhamGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSNhomHang()
        btncapnhatnhomhang.Visible = True
        btnaddnhomhang.Visible = True
    End Sub

    Private Sub btnTimnmh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimnmh.Click
        dgrvDSNhomHang.DataSource = NhomSanPhamBS.GetByName(txtTimnmh.Text)
    End Sub



    '-------- Các Sự Kiện

    Private Sub txtTimnmh_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtTimnmh.MouseClick
        radiotimnmh.Checked = True
    End Sub

    Private Sub radiotimnmh_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles radiotimnmh.MouseClick
        txtTimnmh.Focus()
    End Sub

    Private Sub RadioButton1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RadioButton1.MouseClick
        LoadDGV_DSNhomHang()
    End Sub
End Class